<template>
  <div class="opportunity-list-container">
    <!-- 暂时只对接PC端 -->
    <listOfPc
      v-if="isPC()"
      :list="list"
      :loading="loading"
      :pagination="pagination"
      @page-change="handlePageChange"
      @refresh="refreshData"
    />

    <!-- H5端暂时使用原有组件，后续对接 -->
    <listOfH5
      v-else
      :list="list"
      :loading="loading"
      :refreshing="refreshing"
      :pagination="pagination"
      @page-change="loadMoreData"
      @refresh="refreshData"
    />
  </div>
</template>

<script setup>
import { ref, computed, onMounted, watch } from "vue";
import listOfH5 from "./h5/index.vue";
import listOfPc from "./pc/index.vue";
import { isPC } from "@/utils/platformTool";
import { useCommonStore, useCustomerStore } from "@/store";
import { ElMessage } from "element-plus";
import { getOpportunityListAPI } from "@/services/customer/index.js";

const customerStore = useCustomerStore();
const customerId = computed(() => customerStore.currentCustomerId);

// 响应式数据
const list = ref([]);
const loading = ref(false);
const refreshing = ref(false);
const pagination = ref({
  pageNumber: 1,
  pageSize: 10,
  total: 0,
});

// API请求函数
const fetchOpportunityList = async (params = {}) => {
  if (!customerId.value) {
    console.warn("customerId不存在，无法获取商机列表");
    return;
  }

  loading.value = true;

  const requestParams = {
    customerId: customerId.value,
    pageNumber: params.pageNumber || pagination.value.pageNumber,
    pageSize: params.pageSize || pagination.value.pageSize,
    ...params,
  };

  const data = await getOpportunityListAPI(requestParams);
  pagination.value.total = data.totalNum;
  loading.value = false;
  return data.datas || [];
};

// 分页变化处理
const handlePageChange = async (page, size) => {
  pagination.value.pageNumber = page;
  if (size) {
    pagination.value.pageSize = size;
  }
  list.value = await fetchOpportunityList({
    pageNumber: page,
    pageSize: pagination.value.pageSize,
  });
};

const loadMoreData = async (page, size) => {
  if (finished.value) {
    return;
  }
  loading.value = true;
  const listItems = await getMyOpportunityListAPI({
    pageNumber: pagination.value.pageNumber + 1,
    pageSize: pagination.value.pageSize,
  });
  if (page === 1) {
    list.value = listItems;
  } else {
    list.value = [...list.value, ...listItems];
  }
};

// 刷新数据
const refreshData = async () => {
  refreshing.value = true;
  pagination.value.pageNumber = 1;
  list.value = await fetchOpportunityList({
    pageNumber: 1,
    pageSize: pagination.value.pageSize,
  });
  refreshing.value = false;
};

// 监听customerId变化
watch(
  customerId,
  (newCustomerId) => {
    if (newCustomerId) {
      refreshData();
    } else {
      list.value = [];
    }
  },
  { immediate: false }
);

// 组件挂载时获取数据
onMounted(() => {
  if (customerId.value) {
    refreshData();
  }
});

// 暴露方法给父组件使用
defineExpose({
  refreshData,
  fetchOpportunityList,
});
</script>

<style scoped lang="less">
.opportunity-list-container {
  width: 100%;
  min-height: 200px;
}
</style>
